package cn.ekuma.pos.pda.order;

import java.util.Date;
import java.util.List;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;
import cn.ekuma.android.ui.filter.QuickTimeFilter;
import cn.ekuma.android.ui.filter.TimeCycleChangedListener;
import cn.ekuma.data.ui.android.AbstractViewActivate;
import cn.ekuma.data.ui.android.BaseBeanListSelecter;
import cn.ekuma.data.ui.android.OnBaseBeanSelectedListener;
import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.pos.pda.qkorder.OrderPanelActivity;

public class ShardOrderViewActivate extends AbstractViewActivate implements OnBaseBeanSelectedListener<FindOrdersInfo>, TimeCycleChangedListener{

	private static final int MSG_SUCCESS = 0;
    private static final int MSG_FAILURE = 1;
    private static final int MSG_LOAD_ORDER_SUCCESS=2;
	OrderListAdapter listAdapter;
	private QuickTimeFilter quickTimeFilter;
	
	
	
	public void initAdvSeachView() {
		// TODO Auto-generated method stub
		
	}

	
	public boolean isQuickSeachAble() {
		return true;
	}

	
	public void onQuickSeach(String inputText) {
		// TODO Auto-generated method stub
		
	}

	
	public View createFilterPanel() {
		quickTimeFilter=new QuickTimeFilter(this);
		quickTimeFilter.setTimeCycleChangedListener(this);
    	return quickTimeFilter;
	}

	
	public View createAdvFilterPanel() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public View createContentPanel() {
		BaseBeanListSelecter main=new BaseBeanListSelecter<FindOrdersInfo>(this);
		listAdapter=new OrderListAdapter(this);
		main.setAbstractBaseBeanAdapter(listAdapter);
		main.setOnBaseBeanSelectedListener(this);
		return main;
	}

	
	public void baseBeanSelected(final FindOrdersInfo obj, int position) {
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			
			public void run(){
				I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
				try {
					Order order=dlERP.getSharedOrder(obj.getId());
					if(order!=null)
					   mHandler.obtainMessage(MSG_LOAD_ORDER_SUCCESS, order).sendToTarget();
				} catch (BasicException e) {
					e.printStackTrace();
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
				}
			    dialog.dismiss(); 
		}
		}.start();	
		
		
	}

	
	public void timeCycleChanged(final Date startdate, final Date enddate) {
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			
			public void run(){
				I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
				
				Object[] afilter=createValue();
		        afilter[6] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
		        afilter[7] = startdate;
		        afilter[8] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
		        afilter[9] = enddate;
				try {
					List<FindOrdersInfo> lists=dlERP.listShardOrder(afilter);
					mHandler.obtainMessage(MSG_SUCCESS, lists).sendToTarget();			
				} catch (BasicException e) {
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
				}
				dialog.dismiss(); 
			}
			}.start();		
	}
	
	
	 private Handler mHandler = new Handler() {  
	        public void handleMessage (Message msg) {//此方法在ui线程运行   
	            switch(msg.what) {  
	            case MSG_SUCCESS:  
	            	listAdapter.clear();
					for(FindOrdersInfo info:(List<FindOrdersInfo>)msg.obj)
					listAdapter.add(info);
	                break;  
	            case MSG_FAILURE:  
	                Toast.makeText(getApplication(), "执行出错了", Toast.LENGTH_LONG).show();  
	                break;      
	            case MSG_LOAD_ORDER_SUCCESS:
	            	Intent intent = new Intent();
	            	intent.setClass(ShardOrderViewActivate.this, OrderPanelActivity.class);
	            	Bundle mBundle=new Bundle();
	            	mBundle.putSerializable(OrderPanelActivity.ORDER_TO_SHOW, (Order) msg.obj);
	            	intent.putExtras(mBundle);
	            	startActivity(intent);	
	            	break;
	            }  
	        }  
	    };  

	
	  public Object[] createValue() {
	        Object[] afilter = new Object[18];
	        // Order ID
	        
	            afilter[0] = QBFCompareEnum.COMP_NONE;
	            afilter[1] = null;
	       

	        //Order DefineType
	         
	            afilter[2] = QBFCompareEnum.COMP_NONE;
	            afilter[3] = null;
	        

	        // Sale and refund checkbox
	       
	            afilter[4] = QBFCompareEnum.COMP_NONE;
	            afilter[5] = 2;
	        

	        // Date range
	        Object startdate = null;
	        Object enddate = null;

	        afilter[6] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
	        afilter[7] = startdate;
	        afilter[8] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
	        afilter[9] = enddate;

	        //User
	       
	            afilter[10] = QBFCompareEnum.COMP_NONE;
	            afilter[11] = null;
	        

	        //Customer
	        
	            afilter[12] = QBFCompareEnum.COMP_NONE;
	            afilter[13] = null;
	        

	        //Location
	        
	            afilter[14] = QBFCompareEnum.COMP_NONE;
	            afilter[15] = null;
	        

	        // Receipt money
	        afilter[16] = QBFCompareEnum.COMP_NONE;
	        afilter[17] =null;
	        
	        return afilter;
	    }

	
	public void filterParaReset() {
		quickTimeFilter.resertValues();
		//quickSeachText.setText(null);
	}

}
